package net.hn.hnms.biz.safety.robot.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.common.jpa.utils.ResponsePage;
import net.hn.hnms.biz.safety.robot.domain.HisData;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 智呈物联 pan'sx
 * 2022/10/10 10:27
 * 1.0 version
 */
@RestController
@RequestMapping("riskMonitor/robot")
@Api(value = "机器人巡检", tags = {"机器人巡检"})
@RequiredArgsConstructor
public class RobotController {
//    private final EntityManager robotEntityManager;
    private final JdbcTemplate robotJdbcTemplate;

    @GetMapping("tables")
    @ApiOperation("数据查询-实时数据")
    public R<ResponsePage<HisData>> tables(@RequestParam(defaultValue = "0") int pageNum, @RequestParam(defaultValue = "20") int pageSize, String checkName, String checkState) {
        StringBuffer sqlcount = new StringBuffer("SELECT count(*) FROM \"rb_hisdata\" where 1=1 ");
        StringBuffer sql = new StringBuffer("SELECT * FROM \"rb_hisdata\" where 1=1 ");
        StringBuffer sb = new StringBuffer();
        if (StringUtils.isNotBlank(checkName)) {
            sb.append(" and meteragepointname like '%").append(checkName).append("%' ");
        }
        if (StringUtils.isNotBlank(checkState)) {
            sb.append(" and checkstate = '").append(checkState).append("' ");
        }
        List<HisData> list = new ArrayList<>();
        Integer integer = robotJdbcTemplate.queryForObject(sqlcount.append(sb).toString(), Integer.class);
        if (integer > 0) {
            sql.append(sb).append(" order by dtime desc").append(" limit ").append(pageSize).append(" OFFSET ").append(pageNum*pageSize);
            list = robotJdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(HisData.class));
        }
        ResponsePage<HisData> page = new ResponsePage<HisData>(list, integer);
        return R.ok(page);
    }

}
